package com.dsa.team5.kanban.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dsa.team5.kanban.dao.KanbanDao;
import com.dsa.team5.kanban.model.Kanban;
import com.dsa.team5.kanban.utils.CustomHibernateDaoSupport;

@Repository("kanbanDao")
public class KanbanDaoImpl extends CustomHibernateDaoSupport implements
		KanbanDao {

	@Autowired
	SessionFactory sessionFactory;

	@Override
	public void save(Kanban kanban) {
		getHibernateTemplate().save(kanban);
	}

	@Override
	public void update(Kanban kanban) {
		getHibernateTemplate().update(kanban);
	}

	@Override
	public void delete(Kanban kanban) {
		getHibernateTemplate().delete(kanban);
	}

	@Override
	public Kanban getKanbanByCode(String kanbanCode) {
		@SuppressWarnings("unchecked")
		List<Kanban> results = (List<Kanban>) getHibernateTemplate()
				.findByNamedParam( 
			            "select kb from Kanban kb " +
			            "where kb.code = :kanbanCode", 
			            "kanbanCode", kanbanCode);
		if (results != null && results.size()>0) return results.get(0);
		else return null;
	}
}
